//
//  FFBankcardAddNextVM.m
//  LLWallet
//
//  Created by tt on 2020/9/15.
//

#import "FFBankcardAddNextVM.h"
#import "WKLabelItemCell.h"
#import "FFBankcardAddInputCell.h"
#import "FFBankcardAddVM.h"
#import "FFBankcardListVC.h"
#import "FFWalletConst.h"
@interface FFBankcardAddNextVM ()

@property(nonatomic,strong) NSString *phone;

@property(nonatomic,strong) NSString *name; // 真实姓名
@property(nonatomic,strong) NSString *certNo; //证件号码

@end

@implementation FFBankcardAddNextVM

- (NSArray<NSDictionary *> *)tableSectionMaps {
   NSString *bankname = [FFBankcardAddVM shared].bankcardCheck.data.bankname;
   NSString *bankcardType = [FFBankcardAddVM shared].bankcardCheck.data.bankcardType;
    NSString *bankcardTypeName = LLang(@"储蓄卡");
    if([bankcardType isEqualToString:@"CC"]) {
        bankcardTypeName = LLang(@"信用卡");
    }
    __weak typeof(self) weakSelf = self;
    
    NSMutableArray *items = [NSMutableArray array];
    
    // 银行卡信息
    [items addObject:@{
         @"height":@(20.0f),
         @"title":LLang(@"银行卡信息"),
         @"items":@[
                 @{
                     @"class": WKLabelItemModel.class,
                     @"label":LLang(@"卡类型"),
                     @"value": [NSString stringWithFormat:@"%@ %@",bankname,bankcardTypeName],
                 },
                 @{
                     @"class": FFBankcardAddInputModel.class,
                     @"label":LLang(@"手机号码"),
                     @"placeholder":LLang(@"请输入银行预留的手机号码"),
                     @"keyboardType":@(UIKeyboardTypeNumberPad),
                     @"maxLen":@(11),
                     @"onChange":^(NSString *value){
                         weakSelf.phone = value;
                     }
                 }
         ]
    }];
    
    // 实名认证
    if(![FFBankcardAddVM shared].nameAuthResp) {
        [items addObject:[self nameAuthSectionMap]];
    }
    
    // 确认
    [items addObject:@{
        @"height": @(40.0f),
        @"items": @[
                @{
                    @"class": WKButtonItemModel2.class,
                    @"title": LLang(@"确认"),
                    @"onPressed":^{
                        if(weakSelf.phone.length<=0) {
                            [[WKNavigationManager shared].topViewController.view showHUDWithHide:LLangW(@"银行预留手机号不能为空！", weakSelf)];
                            return;
                        }
                        if(weakSelf.phone.length!=11) {
                            [[WKNavigationManager shared].topViewController.view showHUDWithHide:LLangW(@"银行预留手机号必须为11位！", weakSelf)];
                            return;
                        }
                        if(![FFBankcardAddVM shared].nameAuthResp) {
                            if(weakSelf.name.length<=0) {
                                [[WKNavigationManager shared].topViewController.view showHUDWithHide:LLangW(@"真实姓名不能为空！", weakSelf)];
                                return;
                            }
                            if(weakSelf.certNo.length<=0) {
                                [[WKNavigationManager shared].topViewController.view showHUDWithHide:LLangW(@"证件号不能为空！", weakSelf)];
                                return;
                            }
                        }
                        // 添加银行卡
                         [[WKNavigationManager shared].topViewController.view showHUD];
                        [weakSelf addBankcard].then(^{
                            [[WKNavigationManager shared].topViewController.view hideHud];
                            [[WKNavigationManager shared] popToViewControllerClass:FFBankcardListVC.class animated:YES];
                            
                            [[FFBankcardAddVM shared] clear]; // 清除添加数据
                            // 刷新银行卡列表界面
                            [[NSNotificationCenter defaultCenter] postNotificationName:LIM_NOTIFY_WALLET_BNAKCARD_REFRESH object:nil];
                            
                        }).catch(^(NSError *error){
                            [[WKNavigationManager shared].topViewController.view switchHUDError:error.domain];
                            return;
                        });
                    },
                }
        ],
    }];
    
    return items;
}

// 实名认证信息的section
-(NSDictionary*) nameAuthSectionMap {
    __weak typeof(self) weakSelf = self;
    return @{
         @"height":@(20.0f),
         @"title":@"实名认证信息",
         @"items":@[
                 @{
                     @"class": FFBankcardAddInputModel.class,
                     @"label":LLangW(@"真实姓名", weakSelf),
                     @"placeholder":LLangW(@"请输入真实姓名", weakSelf),
                     @"onChange":^(NSString *value){
                         weakSelf.name = value;
                     }
                 },
                 @{
                     @"class": FFBankcardAddInputModel.class,
                     @"label":LLangW(@"证件类型", weakSelf),
                     @"value": LLangW(@"身份证", weakSelf),
                     @"disable": @(true),
                 },
                 @{
                     @"class": FFBankcardAddInputModel.class,
                     @"label":LLangW(@"证件号码", weakSelf),
                     @"placeholder": LLangW(@"请输入证件号码", weakSelf),
                     @"onChange":^(NSString *value){
                         weakSelf.certNo = value;
                     }
                 }
         ]
    };
}

-(AnyPromise*) addBankcard {
    FFBankcardInfoResp *bankcardInfo = [FFBankcardAddVM shared].bankcardCheck.data;
   return  [[WKAPIClient sharedClient] POST:@"impay/bankcards" parameters:@{
        @"bankcard":[FFBankcardAddVM shared].bankcard?:@"",
        @"bankcard_type": bankcardInfo.bankcardType?:@"",
        @"bankcode": bankcardInfo.bankcode?:@"",
        @"bankname": bankcardInfo.bankname?:@"",
        @"phone": self.phone?:@"",
        @"cert_type":@(1),
        @"cert_no":self.certNo?:@"",
        @"holder_name": self.name?:@"",
        
    }];
}

@end
